﻿using System;
using System.Diagnostics;
using System.Text;
using Microsoft.AnalysisServices.AdomdClient;
using Microsoft.Practices.Unity;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moop.Common;
using Moop.DataLayer.Olap.Service;

namespace Moop.DataLayer.Olap.Service.Test
{
    [TestClass]
    public class MdxQueriesTests
    {
        public string CONN_STRING = @"Data Source=.;Catalog=Adventure Works DW 2008R2;";
        public string m_MdxCommand = @"SELECT {[Product].[Category].[All Products]} ON COLUMNS ,{[Customer].[Customer].MEMBERS} ON ROWS FROM [Adventure Works]";


        [TestMethod]
        public void RunCommand()
        {

            StringBuilder result = new StringBuilder();

            #region Fetch Data via AdomdDataReader

            using (AdomdConnection conn = new AdomdConnection(CONN_STRING))
            {
                conn.Open();

                AdomdCommand cmd = new AdomdCommand(m_MdxCommand, conn);

                AdomdDataReader reader = cmd.ExecuteReader();

                if (reader.RecordsAffected > 0)
                {
                    while (reader.Read())
                    {
                        for (int i = 0; i < reader.FieldCount; i++)
                        {
                            var cell = reader[i];
                            Debug.WriteLine(cell);
                        }
                    }
                }

                reader.Close();
                conn.Close();
            }

            #endregion


            #region Fetch Data via Execute CellSet

            result.Clear(); //Clear SB

            using (AdomdConnection conn = new AdomdConnection(CONN_STRING))
            {
                try
                {
                    conn.Open();

                    AdomdCommand cmd = new AdomdCommand(m_MdxCommand, conn);
                    CellSet cellSetResult = cmd.ExecuteCellSet();

                    TupleCollection tuplesOnColumns = cellSetResult.Axes[0].Set.Tuples;

                    foreach (Microsoft.AnalysisServices.AdomdClient.Tuple column in tuplesOnColumns)
                    {
                        result.Append(column.Members[0].Caption + "\t");

                    }
                    conn.Close();
                }
                catch (Exception e)
                {

                }
            }

            #endregion


            #region Fetch Data via DataTable


            //using (AdomdConnection conn = new AdomdConnection(CONN_STRING))
            //{
            //    conn.Open();
            //    var mdxQuery = new StringBuilder();
            //    mdxQuery.Append(m_MdxCommand);

            //    using (AdomdCommand cmd = new AdomdCommand(mdxQuery.ToString(), conn))
            //    {
            //        DataSet ds = new DataSet();
            //        ds.EnforceConstraints = false;
            //        ds.Tables.Add();
            //        DataTable dt = ds.Tables[0];
            //        dt.Load(cmd.ExecuteReader());
            //    }
            //}


            #endregion
        }


        /// <summary>
        /// This method uses the Microsoft.AnalysisServices.AdomdClient Assembly/NS 
        /// 
        ///     Namespace: Microsoft.AnalysisServices.AdomdClient
        ///     Assembly:  Microsoft.AnalysisServices.AdomdClient (in Microsoft.AnalysisServices.AdomdClient.dll)
        /// 
        /// BUT!!! In my code I will beusing this Assembly just to populate DIMS / MG etc
        /// Use following Instead:
        /// 
        ///     Namespace: Microsoft.AnalysisServices
        ///  Assembly:  Microsoft.AnalysisServices (in Microsoft.AnalysisServices.dll)
        ///
        /// </summary>
        /*[TestMethod]
        public void GetDimData()
        {
            StringBuilder result = new StringBuilder();

            using (AdomdConnection conn = new AdomdConnection(CONN_STRING))
            {
                conn.Open();

                //Loop through every cube
                foreach (CubeDef cube in conn.Cubes)
                {
                    //Skip hidden cubes.
                    if (cube.DastabaseName.StartsWith("$"))
                        continue;

                    //Write the cube name
                    result.AppendLine(cube.DastabaseName);

                    //Write out all dimensions, indented by a tab.
                    foreach (Microsoft.AnalysisServices.AdomdClient.Dimension dim in cube.Dimensions)
                    {
                        result.Append("\t");
                        result.AppendLine(dim.DastabaseName);

                        foreach (var hire in dim.Hierarchies)
                        {
                            result.Append("\t");
                            result.AppendLine(hire.UniqueName);
                        }

                    }
                }
                //Close the connection
                conn.Close();
            }
            result.ToString();
        }
         * */

    }
}
